

import java.util.concurrent.TimeUnit;

public class DoubleLockException {


    public static void main(String[] args) throws InterruptedException {

        System.out.println(Runtime.getRuntime().availableProcessors());

        Object lockA = new Object();
        Object lockB = new Object();

//        Lock lockA = new ReentrantLock();
//        Lock lockB = new ReentrantLock();

        new Thread(() -> {
            synchronized (lockA) {
                System.out.println("拥有lockA锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lockB) {
                    System.out.println("获得B锁");
                }
            }

        }, "A").start();

//        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            synchronized (lockB) {
                System.out.println("拥有lockB锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lockA) {
                    System.out.println("获得A锁");
                }
            }

        }, "B").start();
    }

}
